#!/usr/bin/env python
# coding: utf-8

'''每天定时计算hotfeet，存入到redis备用
作为可独立执行作业，不与项目中任何功能有调用关系

    rate = tfeet.like*0.2 + tfeet.comments*0.6
    order by rate desc limit 7

Created on 2011-9-1

@author: zc
'''

import redis
import MySQLdb
import os

#os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'



""" redis connect settings """
dbhost = "127.0.0.1"
dbname = 1 #hotfeet db
port   = 6379
r = redis.Redis(host=dbhost, db=dbname, port=port)


""" mysql connect settings"""
dbuser = "root"
dbpwd  = "12345"
dbhost = "127.0.0.1"
dbname = "tinyfeet"
port   = 3306
connection = MySQLdb.connect(host=dbhost, user=dbuser, passwd=dbpwd, db=dbname, port=port,charset='utf8')
cursor     =  connection.cursor()



""" 变量设置"""
key   = "hotfeet"
score = 0

""" 从mysql 取出最热7条hotfeet"""

sql = """
      select place, longitude, latigude, t.like*2 + c.cnt*6 as rate
      from tfeet t, (select tfid, count(*) as cnt
  	  		         from comments group by tfid
               ) c
      where t.tfid = c.tfid
      order by rate desc
      limit 7
      """

cursor.execute(sql)

hotfeet_val = ""
hotfeet = [] 
if cursor:
	 for op in cursor.fetchall():
		 hotfeet_val = op[0] +","+ str(op[1]) + "," + str(op[2])
#		 print hotfeet_val  
		 """ 写入到redis待用"""
		 r.zadd(key,hotfeet_val,op[3])

cursor.close()
connection.close()

""" test code"""
#t = r.zrevrange("hotfeet", 0, -1)
#for a in t:
#	print a#.split(',')
 
